<?php
include "otamu_config.php";

define("_VIEWPATH_","views/jkr/");
$layout = "layout";

$OBJECT = 'master_institusi';
$OBJECT_TABLE = $OBJECT;

$TABLE_GURU = "guru_staf";

if (!isset($_GET['act'])) $_GET['act'] = ''; 

class Institusi extends ADOdb_Active_Record
{
	var $_table = "master_institusi";
}

class MaklumatPrasekolah extends ADOdb_Active_Record 
{ 
	var $_table = "master_institusi_prasekolah";  	
}

class Organisasi extends ADOdb_Active_Record 
{ 
	var $_table = "organisasi";  	
}

class MasterProfile extends ADOdb_Active_Record
{
	var $_table = "master_profile";
}

class Murid extends ADOdb_Active_Record
{
	var $_table = "murid";
}

class Ibubapa extends ADOdb_Active_Record
{
	var $_table = "parent";
}

class MasterAlamat extends ADOdb_Active_Record
{
	var $_table = "master_alamat";
}

class ProfileJPN extends ADOdb_Active_Record
{
	var $_table = "ref_jpn_records";
}

class GuruEGTukar extends ADOdb_Active_Record
{
	var $_table = "ref_egtukar_record";
}

class MasterStaf extends ADOdb_Active_Record
{
	var $_table = "guru_staf";
}
 
class Kelas extends ADOdb_Active_Record
{
	var $_table = "master_kelas";
}
class TransKelasGuru extends ADOdb_Active_Record
{
	var $_table = "trans_kelas_guru";
}
 

switch ($_GET['act']) {

	case "new_projek_am";
	
	break;
	case "edit_projek_rancang";
	
	break;
	case "edit_projek_reka";
	
	break;
	case "edit_projek_perolehan";
	
	break;
	case "edit_projek_bina";
	
	break;
	case "edit_projek_dlp";
	
	break;

	case "new_guru_egtukar" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
		
		$guru_egtukar = new GuruEGTukar();
		$guru_egtukar->Load("no_kp_passport='$_REQUEST[no_kp]'");
		
		$master_profile = new MasterProfile();
		$master_profile->no_kp = $guru_egtukar->no_kp_passport;
		$master_profile->nama  = $guru_egtukar->nama;
		$master_profile->kod_kaum  = $guru_egtukar->kod_kaum;
		$master_profile->jantina_id = ($guru_egtukar->kod_jantina=='L')? "1":"2";
				
		$master_staf = new MasterStaf();
		$master_staf->no_fail_jpn = $guru_egtukar->nofailjpn;
		$master_staf->tarikh_mula_berkhidmat = $guru_egtukar->tarikh_mula_berkhidmat;
		
		$master_staf->kod_kategori = "01";
		$master_staf->kod_akademik_tinggi = $guru_egtukar->kod_akademik_tinggi;
		$master_staf->kod_gred_jawatan    = $guru_egtukar->kod_gred_jawatan;
		$master_staf->kod_taraf_jawatan   = $guru_egtukar->kod_taraf_jawatan;
		$master_staf->kod_opsyen          = $guru_egtukar->kod_opsyen;
				//print_r($guru_egtukar);exit;
		break;

	case "new_guru" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
			
		$master_profile = new MasterProfile();
		if (isset($_REQUEST['no_kp'])) $master_profile->no_kp = $_REQUEST['no_kp'];
				
		$master_staf = new MasterStaf();
		
		$master_staf->kod_kategori = "02";
		if (isset($_REQUEST['pilih_kod_kategori'])) {
			$master_staf->kod_kategori = $_REQUEST['pilih_kod_kategori'];
		}
				//print_r($guru_egtukar);exit;
		break;

	case "edit_staf" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
			
		$master_profile = new MasterProfile();
		$master_profile->Load("no_kp='$_REQUEST[no_kp]'");
		
		$master_staf = new MasterStaf();
		$master_staf->Load("master_profile_id='$master_profile->id'");
		break;

	case "view_staf" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$guru_egtukar = new GuruEGTukar();
		$guru_egtukar->Load("no_kp_passport='$_REQUEST[no_kp]'");

		$master_profile = new MasterProfile();
		$master_profile->Load("no_kp='$_REQUEST[no_kp]'");
		

		$master_staf = new MasterStaf();
		$master_staf->Load("master_profile_id='$master_profile->id'");

		break;
	
	case "popup_view_staf" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$guru_egtukar = new GuruEGTukar();
		$guru_egtukar->Load("no_kp_passport='$_REQUEST[no_kp]'");

		$master_profile = new MasterProfile();
		$master_profile->Load("no_kp='$_REQUEST[no_kp]'");
		

		$master_staf = new MasterStaf();
		$master_staf->Load("master_profile_id='$master_profile->id'");

		include _VIEWPATH_."layout_popup.php";exit;

		break;
	
	case "create_guru_kpm" :
		
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
		
		//print_r($_REQUEST);exit;

		//create profile guru
		$master_profile = new MasterProfile();
		$no_kp = $_REQUEST['master_profile']['no_kp'];
		$master_profile->Load("no_kp='$no_kp'");
		foreach ($_REQUEST[master_profile] as $k => $v) {
			$master_profile->$k = $v;
			//if (empty($v)) $master_profile->$k = NULL;
		}
		if (empty($master_profile->no_surat_beranak)) $master_profile->no_surat_beranak = NULL;
		if (empty($master_profile->no_passport)) $master_profile->no_passport = NULL;

		if (!$master_profile->save()) { 
			$_SESSION['error'] = "Profile : " . $master_profile->ErrorMsg();
			$act = "new_guru_egtukar";
			break;
		}

		//create staf
		$master_staf = new MasterStaf();
		foreach ($_REQUEST[master_staf] as $k => $v) {
			$master_staf->$k = $v;
			if (empty($v)) $master_staf->$k = NULL;
		}
		if ($master_staf->current_kelas_id == '') $master_staf->current_kelas_id = NULL;
		$master_staf->master_profile_id = $master_profile->id;
		$master_staf->tarikh_mula_berkhidmat = ymd_date($master_staf->tarikh_mula_berkhidmat);
		$master_staf->tarikh_mula_pra = ymd_date($master_staf->tarikh_mula_pra);
		//print_r($master_staf);exit;
		
		if (!$master_staf->save()) { 
			$_SESSION['error'] .= "<br>Murid: ".$master_staf->ErrorMsg();
			$act = "new_guru_egtukar";
			$master_profile->delete();
			break;
		}
		
	
		//$obj->Load('id='.$_REQUEST['id']);
		$_SESSION['flash'] = 'Staf telah berjaya ditambah.';
		header("Location: ?act=view_staf&no_kp=$master_profile->no_kp");
		break;

	case "update_staf" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
		
		//print_r($_REQUEST);exit;

		//create profile guru
		$master_profile = new MasterProfile();
		$no_kp = $_REQUEST['master_profile']['no_kp'];
		$master_profile->Load("no_kp='$no_kp'");
		//print_r($master_profile);exit;
		foreach ($_REQUEST[master_profile] as $k => $v) {
			$master_profile->$k = $v;
			//if (empty($v)) $master_profile->$k = NULL;
		}
		if (empty($master_profile->no_surat_beranak)) $master_profile->no_surat_beranak = NULL;
		if (empty($master_profile->no_passport)) $master_profile->no_passport = NULL;

		//print_r($master_profile);exit;
		if (!$master_profile->save()) { 
			$_SESSION['error'] = "Profile : " . $master_profile->ErrorMsg();
			$act = "edit_staf";
			break;
		}

		//create staf
		$master_staf = new MasterStaf();
		$master_staf->Load("master_profile_id='$master_profile->id'");
		foreach ($_REQUEST[master_staf] as $k => $v) {
			$master_staf->$k = $v;
			if (empty($v)) $master_staf->$k = NULL;
		}

		if ($master_staf->current_kelas_id == '') $master_staf->current_kelas_id = NULL;
		$master_staf->master_profile_id = $master_profile->id;
		$master_staf->tarikh_mula_berkhidmat = ymd_date($master_staf->tarikh_mula_berkhidmat);
		$master_staf->tarikh_mula_pra        = ymd_date($master_staf->tarikh_mula_pra);

		if (!$master_staf->save()) { 
			$_SESSION['error'] .= "<br>Staf: ".$master_staf->ErrorMsg();
			$act = "edit_staf";
			break;
		}
		
		//$obj->Load('id='.$_REQUEST['id']);
		$_SESSION['flash'] = 'Maklumat staf telah berjaya dikemaskini.';
		header("Location: ?act=view_staf&no_kp=$master_profile->no_kp");
		break;

	case "new_guru_step1" :
		break;
	case "new_guru_step2" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
		//print_r($institusi);exit;

		if (empty($_REQUEST['no_kp'])) {
			exitError("Sila masukkan No. Kad Pengenalan.");
		}
		
		//check if already exists in SPK db
		$sql = "SELECT * FROM guru_staf s INNER JOIN master_profile p ON p.id=s.master_profile_id
		         WHERE p.no_kp = '$_REQUEST[no_kp]'
			   ";
		$res = pg_query($sql);
		$exist_count = pg_num_rows($res);

		if ($exist_count == 0) { //takde dalam SPK
			//check egtukar , KPM in egtukar cannot enter Swasta!
			$sql = "SELECT * FROM ref_egtukar_record WHERE no_kp_passport='$_REQUEST[no_kp]'";
			$res_eg = pg_query($sql);
			$egtukar_count = pg_num_rows($res_eg);
			
			if ($egtukar_count == 0) {
				header("Location: index.jsp?act=new_guru&no_kp=$_REQUEST[no_kp]&pilih_kod_kategori=$_REQUEST[pilih_kod_kategori]");					
			}
			else { //data ada dalam egtukar, ni guru KPM ni...
				if ($institusi->agensi_pelaksana_id != '1') {
					//guru KPM cuba memasuki sekolah bukan KPM, tak cukup gaji ke?...
					exitError("Guru yang berdaftar sebagai guru KPM tidak boleh mengajar di sekolah swasta. Kalau nak juga bule tapi senyap2.");
				}
				else {
					header("Location: index.jsp?act=new_guru_egtukar&no_kp=$_REQUEST[no_kp]");
				} 
				
			}

		}
		else {
			$act = "new_guru_step1";
		}

		break;


	////////////////////////murid////////////////////////////////
	case "kick_murid_from_kelas" :
	//print_r($_REQUEST);exit;
		$in_arr = join(",",$_REQUEST['murid_ids']);
		$sql = "UPDATE murid SET current_kelas_id=null WHERE murid_id IN ($in_arr)";
		//echo $sql;exit;
		$res = pg_query($sql);

		$_SESSION['flash'] = 'Murid telah didaftarkan keluar dari kelas';
		header("Location: ?act=list_murid_of_kelas&institusi_id=$_REQUEST[institusi_id]&id=$_REQUEST[id]");
		break;

	case "assign_guru_to_kelas" :
		$sql = "SELECT * FROM master_kelas WHERE id='$_REQUEST[kelas_id]' AND guru_id is not null";
		//echo $sql;exit;
		$res = pg_query($sql);
		if (pg_num_rows($res) < 0) {
			exitError("dah ada cikgu..");
		}else {
			$kelas = new Kelas();
			$kelas->Load('id='.$_REQUEST['kelas_id']);
			$kelas->guru_id = $_REQUEST[guru_id];
			$kelas->save();
			
			$trans_kelas_guru = new TransKelasGuru();
			$trans_kelas_guru->kelas_id = $_REQUEST['kelas_id'];
			$trans_kelas_guru->guru_id = $_REQUEST['guru_id'];
			$trans_kelas_guru->tahun = Date("Y");
			$trans_kelas_guru->tarikh_mula = Date("Y-m-d");
			$trans_kelas_guru->tarikh_tamat = null;
			$trans_kelas_guru->created_by = null;
			$trans_kelas_guru->created_at = Date("Y-m-d") . " " . Date("H:i:s"); 
			$trans_kelas_guru->save();

			$_SESSION['flash'] = 'Guru telah didaftarkan sebagai guru kelas';
			header("Location: ?act=popup_new_guru_to_kelas&institusi_id=$kelas->institusi_id&kelas_id=$_REQUEST[kelas_id]");			
		}
		break;

	case "assign_pembantu_to_kelas" :
		$sql = "SELECT * FROM master_kelas WHERE id='$_REQUEST[kelas_id]' AND guru_id is not null";
		//echo $sql;exit;
		$res = pg_query($sql);
		if (pg_num_rows($res) < 0) {
			exitError("dah ada cikgu..");
		}else {
			$kelas = new Kelas();
			$kelas->Load('id='.$_REQUEST['kelas_id']);
			$kelas->pembantu_id = $_REQUEST[pembantu_id];
			$kelas->save();
			
			$trans_kelas_guru = new TransKelasGuru();
			$trans_kelas_guru->kelas_id = $_REQUEST['kelas_id'];
			$trans_kelas_guru->guru_id = $_REQUEST['pembantu_id'];
			$trans_kelas_guru->is_pembantu = '1';
			$trans_kelas_guru->tahun = Date("Y");
			$trans_kelas_guru->tarikh_mula = Date("Y-m-d");
			$trans_kelas_guru->tarikh_tamat = null;
			$trans_kelas_guru->created_by = null;
			$trans_kelas_guru->created_at = Date("Y-m-d") . " " . Date("H:i:s"); 
			$trans_kelas_guru->save();

			$_SESSION['flash'] = 'Staf telah didaftarkan sebagai pembantu kelas';
			header("Location: ?act=popup_new_pembantu_to_kelas&institusi_id=$kelas->institusi_id&kelas_id=$_REQUEST[kelas_id]");			
		}
		break;

	case "assign_murid_to_kelas" :
	
		//print_r($_REQUEST);exit;

		$quantity = getClassCapacity($_REQUEST[kelas_id]);
		$max_capacity = 10;
		$to_enroll = count($_REQUEST['murid_ids']);
		
		$beza = ($quantity + $to_enroll) - $max_capacity;
		if ( $beza > 0) {
			exitError("Jumlah murid melebihi kapasiti maksimum kelas. (Lebih $beza orang)");
		}
		$in_arr = join(",",$_REQUEST['murid_ids']);
		$sql = "UPDATE murid SET current_kelas_id=$_REQUEST[kelas_id] WHERE murid_id IN ($in_arr)";
		//echo $sql;exit;
		$res = pg_query($sql);

		$_SESSION['flash'] = 'Murid telah didaftarkan ke dalam kelas';
		header("Location: ?act=popup_new_murid_to_kelas&institusi_id=$_REQUEST[institusi_id]&kelas_id=$_REQUEST[kelas_id]");
		break;

	case "list_murid_of_kelas" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		
		$kelas = new Kelas();
		$kelas->Load('id='.$_REQUEST['id']);
		
		//find kuantiti in class
		$sql = "SELECT count(*) FROM murid WHERE current_institusi_id='$_REQUEST[institusi_id]' 
		           AND current_kelas_id='$_REQUEST[id]'";
		$res = pg_query($sql);
		$quantity = pg_num_rows($res);
		

		//get list murid of this class	
		$sql = "SELECT * FROM vw_murid
			        WHERE current_institusi_id='$_REQUEST[institusi_id]' AND 
					      current_kelas_id='$_REQUEST[id]'
					ORDER BY nama";
		$res_murid_of_kelas = pg_query($sql);
		break;
				
	case "popup_new_murid_to_kelas" ;
		//get list murid of school who has not been assigned any class yet	
		$sql = "SELECT * FROM vw_murid
			        WHERE current_institusi_id='$_REQUEST[institusi_id]' AND 
					      current_kelas_id is null
					ORDER BY nama";
		$res_murid_of_school_no_class = pg_query($sql);

		include _VIEWPATH_."layout_popup.php";exit;
		break;
		
	case "list_murid" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
			
		//prasekolah is default search for SMPK
		//$where_arr[] = "i.prasekolah = '$_REQUEST[prasekolah]'" ;
		$where_arr[] = "current_institusi_id = '$_REQUEST[institusi_id]'" ;

		$where_clause = "WHERE " . join(" AND ", $where_arr);

		if (empty($_REQUEST['p'])) {
			$page = 1;
			$range = "OFFSET 0 LIMIT 20";
		}
		else {
			$page = $_REQUEST['p'];
			$offset = ($page-1) * 20;
			$range = "OFFSET $offset LIMIT 20";			
		}

		$sql_total_cnt = "SELECT count(murid_id) FROM murid i $where_clause";
		$res = pg_query($sql_total_cnt);
		$dat = pg_fetch_object($res,0);
		$total_count = $dat->count;
		$last_page = floor($total_count/20) + 1;

		$sql = "SELECT * FROM murid INNER JOIN master_profile p ON p.id=murid.master_profile_id $where_clause ORDER by nama";
				//echo $sql;exit;
		$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		break;

	case "edit_daftar_murid" :
		$obj = new ProfileJPN();

		$obj->Load('id='.$_REQUEST['id']);
		
		//print_r($obj);exit;
		$jpn_profile = $obj;
		break;

	case "popup_view_murid" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$murid = new Murid();
		if (isset($_REQUEST['nomatrik'])) {
			$murid->Load("nomatrik='$_REQUEST[nomatrik]'");
		}
		if (isset($_REQUEST['no_kp'])) {
			$murid->Load("nomatrik='$_REQUEST[no_kp]'");
		}

		$master_profile = new MasterProfile();
		$master_profile->Load('id='.$murid->master_profile_id);

		$master_alamat = new MasterAlamat();
		$master_alamat->Load('master_profile_id='.$murid->master_profile_id . " AND jenis_alamat='1'");
		
		$bapa = new Ibubapa();
		$bapa->Load("child_profile_id=$master_profile->id AND hubungan_id='1'");

		$profile_bapa = new MasterProfile();
		$profile_bapa->Load("id=$bapa->master_profile_id");

		$ibu = new Ibubapa();
		$ibu->Load("child_profile_id=$master_profile->id AND hubungan_id='2'");

		$profile_ibu = new MasterProfile();
		$profile_ibu->Load("id=$ibu->master_profile_id");

		//$murid->Load('id='.$_REQUEST['id']);
		include _VIEWPATH_."layout_popup.php";exit;
		break;

	case "popup_view_muridJPN" :
		$obj = new ProfileJPN();

		$obj->Load('id='.$_REQUEST['id']);
		include _VIEWPATH_."layout_popup.php";exit;
		break;

	case "new_murid_step1" :

		if (empty($_REQUEST['no_kp']) && empty($_REQUEST['no_sijil_kelahiran'])) {
			$res_count = 0;
		}
		else {
			$where_arr = array();
			
			if ($_REQUEST['no_kp'] != '') {
				$where_arr[] = "no_kp_baru = '$_REQUEST[no_kp]'";
			}
			else {
				$where_arr[] = "no_surat_beranak = '$_REQUEST[no_surat_beranak]'";
			}

			$where_clause = "WHERE " . join(" AND ", $where_arr);
			
			//echo $where_clause;exit;
			
			//if already exist in spk db
			$sql = "SELECT * FROM master_profile 
					 $where_clause 
					 ORDER BY nama $range";
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);
			
			if ($res_count > 0) {
				$jpn_count = 0;
				break;
			}else {
	
				//if student not exist in spk murid table, now check JPN table
				$jpn_sql = "SELECT * FROM ref_jpn_records 
						 where no_kp = '$_REQUEST[no_kp]' OR no_surat_beranak = '$_REQUEST[no_surat_beranak]'
						 ORDER BY nama $range";
				$jpn_res = pg_query($jpn_sql);
				$jpn_count = pg_num_rows($jpn_res);
				//echo $jpn_count;exit;
				if ($jpn_count < 1) { 
					header("Location: index.jsp?act=new_murid");
				}
			}
		}
		break;

	case "new_murid_step2" :

		if (empty($_REQUEST['jenis_dokumen'])) {
			exitError("Sila pilih jenis dokumen.");
		}

		if (($_REQUEST['jenis_dokumen']) == '1') {
			if ($_REQUEST['no_kp'] != '') {
				$where_arr[] = "no_kp = '$_REQUEST[no_kp]'";
				$getstring_param = "no_kp=$_REQUEST[no_kp]";
			}else {
				exitError("Sila masukkan No. MyKid.");
			}
		}
		else if (($_REQUEST['jenis_dokumen']) == '2') {
			if ($_REQUEST['no_surat_beranak'] != '') {
				$where_arr[] = "no_surat_beranak = '$_REQUEST[no_surat_beranak]'";
				$getstring_param = "no_surat_beranak=$_REQUEST[no_surat_beranak]";
			}else {
				exitError("Sila masukkan No. Sijil Lahir.");
			}
		}
		
		else if (($_REQUEST['jenis_dokumen']) == '3') {
			if ($_REQUEST['no_passport'] != '') {
				$where_arr[] = "no_passport = '$_REQUEST[no_passport]'";
				header("Location: index.jsp?act=new_murid&no_passport=$_REQUEST[no_passport]");
				break;
			}else {
				exitError("Sila masukkan No. Passport");
			}
		}
		else {
			//new entry for murid
			header("Location: index.jsp?act=new_murid");
		}

		$where_clause = "WHERE " . join(" AND ", $where_arr);	
		//echo $where_clause;exit;
		
		//if already exist in spk db
		$sql = "SELECT * FROM master_profile p INNER JOIN murid m ON m.master_profile_id=p.id
				 $where_clause 
				 ORDER BY nama $range";
		$res = pg_query($sql);
		$res_count = pg_num_rows($res);
		//echo $sql;exit;
		//echo $res_count;exit;
		
		if ($res_count > 0) { //if murid exist in spk
			$jpn_count = 0;
			break;
		}else {
			//if student not exist in spk murid table, now check JPN table
			if (($_REQUEST['jenis_dokumen']) == '1') {
				$jpn_sql = "SELECT * FROM ref_jpn_records 
						 WHERE no_kp='$_REQUEST[no_kp]'
						 ORDER BY nama $range";
						 //echo $jpn_sql;exit;
				$jpn_res = pg_query($jpn_sql);
				$jpn_count = pg_num_rows($jpn_res);

				if ($jpn_count == 0) { 
					header("Location: index.jsp?act=new_murid&no_kp=$_REQUEST[no_kp]");
				}else {
					header("Location: index.jsp?act=new_murid_jpn&$getstring_param");			
				}
			}
			else if (($_REQUEST['jenis_dokumen']) == '2') {
				$jpn_sql = "SELECT * FROM ref_jpn_records 
						 WHERE no_surat_beranak='$_REQUEST[no_surat_beranak]'
						 ORDER BY nama $range";
						 //echo $jpn_sql;exit;
				$jpn_res = pg_query($jpn_sql);
				$jpn_count = pg_num_rows($jpn_res);

				if ($jpn_count == 0) { 
					header("Location: index.jsp?act=new_murid&no_surat_beranak=$_REQUEST[no_surat_beranak]");
				}else {
					header("Location: index.jsp?act=new_murid_jpn&$getstring_param");			
				}
			}
			else if (($_REQUEST['jenis_dokumen']) == '3') {
				header("Location: index.jsp?act=new_murid&no_passport=$_REQUEST[no_passport]");
			}
			else {
					header("Location: index.jsp?act=new_murid");			
			}

		}
		break;

	case "confirm_daftar_murid" :
		$obj = new ProfileJPN();

		$obj->Load('id='.$_REQUEST['id']);

		break;

	case "new_murid_jpn" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
		//print_r($_REQUEST);exit;

		$master_profile = new ProfileJPN();
		if (isset($_REQUEST['no_kp'])) {
			$master_profile->Load("no_kp='$_REQUEST[no_kp]'");
		}
		if (isset($_REQUEST['no_surat_beranak'])) {
			$master_profile->Load("no_surat_beranak='$_REQUEST[no_surat_beranak]'");
		}
		//$master_profile = new MasterProfile();
		$master_profile->jantina_id = ($master_profile->jantina=='L')? "1":"2";
		$master_profile->agama_id = $master_profile->agama;
		$master_profile->warganegara_id = $master_profile->taraf_warganegara;
		//$master_profile->keturunan_id = (0+getFieldByWhereSql("tkkaum",'kumpulan_kaum',"where kodkaum='$master_profile->keturunan'"));	
		$master_profile->kod_kaum = $master_profile->keturunan;
		
		$master_alamat = new MasterAlamat();
		$master_alamat->alamat1 = $master_profile->alamat1;
		$master_alamat->alamat2 = $master_profile->alamat2;
		$master_alamat->alamat3 = $master_profile->alamat3;
		$master_alamat->poskod = $master_profile->poskod;
		$master_alamat->bandar = $master_profile->bandar;
		$master_alamat->negeri_id = $master_profile->negeri;
		
		//default value murid
		$murid = new Student();
		$murid->status_murid = '1';
		$murid->oku_id = '1';
		
		$profile_bapa = new MasterProfile();
		$profile_bapa->nama  = $master_profile->nama_bapa;
		$profile_bapa->no_kp = $master_profile->no_kp_bapa;
		$profile_bapa->warganegara_id = $master_profile->taraf_warganegara_bapa;

		$profile_ibu = new MasterProfile();
		$profile_ibu->nama  = $master_profile->nama_ibu;
		$profile_ibu->no_kp = $master_profile->no_kp_ibu;
		$profile_ibu->warganegara_id = $master_profile->taraf_warganegara_ibu;
		break;

	case "new_murid" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$master_profile = new MasterProfile();
		if (isset($_REQUEST['no_kp'])) {
			$master_profile->no_kp = $_REQUEST['no_kp'];
		}
		if (isset($_REQUEST['no_passport'])) {
			$master_profile->no_passport = $_REQUEST['no_passport'];
		}
		if (isset($_REQUEST['no_surat_beranak'])) {
			$master_profile->no_surat_beranak = $_REQUEST['no_surat_beranak'];
		}
		$master_alamat = new MasterAlamat();
		$profile_bapa = new MasterProfile();
		$profile_ibu = new MasterProfile();

		//default value murid
		$murid = new Student();
		$murid->status_murid = '1';
		$murid->oku_id = '1';

		break;

	case "create_murid" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		//create profile murid
		$master_profile = new MasterProfile();
		foreach ($_REQUEST[master_profile] as $k => $v) {
			$master_profile->$k = $v;
			//if (empty($v)) $master_profile->$k = NULL;
		}
		if (empty($master_profile->no_surat_beranak)) $master_profile->no_surat_beranak = NULL;
		if (empty($master_profile->no_passport)) $master_profile->no_passport = NULL;
		$master_profile->tarikh_lahir = ymd_date($master_profile->tarikh_lahir);
		//print_r($master_profile);exit;
		if (!$master_profile->save()) { 
			//exitError($master_profile->ErrorMsg());
			$_SESSION['error'] = "Profile : " . $master_profile->ErrorMsg();
			$act = "new_murid";
			break;
		}
		
		//create alamat murid
		$master_alamat = new MasterAlamat();
		foreach ($_REQUEST[master_alamat] as $k => $v) {
			$master_alamat->$k = $v;
			if (empty($v)) $master_alamat->$k = NULL;
		}
		$master_alamat->jenis_alamat = '1';
		$master_alamat->master_profile_id = $master_profile->id;
		if (!$master_alamat->save()) { 
			$_SESSION['error'] .= "<br>Alamat: ".$master_alamat->ErrorMsg();
			$act = "new_murid";
			$master_profile->delete();
			break;
		}
						
		//create murid
		$murid = new Murid();
		foreach ($_REQUEST[murid] as $k => $v) {
			$murid->$k = $v;
			if (empty($v)) $murid->$k = NULL;
		}
		if ($murid->current_kelas_id == '') $murid->current_kelas_id = NULL;
		$murid->master_profile_id = $master_profile->id;
		$murid->nomatrik = $master_profile->no_kp;
		if (!$murid->save()) { 
			$_SESSION['error'] .= "<br>Murid: ".$murid->ErrorMsg();
			$act = "new_murid";
			$master_profile->delete();
			$master_alamat->delete();
			break;
		}
		
		//print_r($murid);exit;
		
		//create profile bapa
		$profile_bapa = new MasterProfile();
		foreach ($_REQUEST[profile_bapa] as $k=>$v) {
			$profile_bapa->$k = $v;
			if (empty($v)) $profile_bapa->$k = NULL;
		}
		$profile_bapa->jantina_id = '1';
		if (!$profile_bapa->save()) { 
			$_SESSION['error'] .= "<br>Profile Bapa: ".$profile_bapa->ErrorMsg();
			$act = "new_murid";
			$master_profile->delete();
			$master_alamat->delete();
			$murid->delete();
			break;
		}

		//create bapa
		$bapa = new Ibubapa();
		foreach ($_REQUEST[bapa] as $k=>$v) {
			$bapa->$k = $v;
			if (empty($v)) $bapa->$k = NULL;
		}
		$bapa->child_profile_id = $master_profile->id;
		$bapa->master_profile_id = $profile_bapa->id;
		$bapa->hubungan_id = '1';
		$bapa->kerja_id = NULL;
		$bapa->pendapatan = NULL;
		if (!$bapa->save()) { 
			$_SESSION['error'] .= "<br>Bapa: ".$bapa->ErrorMsg();
			$act = "new_murid";
			$master_profile->delete();
			$master_alamat->delete();
			$murid->delete();
			$profile_bapa->delete();
			break;
		}
		//print_r($bapa);exit;

		//create profile ibu
		$profile_ibu = new MasterProfile();
		foreach ($_REQUEST[profile_ibu] as $k=>$v) {
			$profile_ibu->$k = $v;
			if (empty($v)) $profile_ibu->$k = NULL;
		}
		$profile_ibu->jantina_id = '1';
		if (!$profile_ibu->save()) { 
			$_SESSION['error'] .= "<br>Profile Ibu: ".$profile_ibu->ErrorMsg();
			$act = "new_murid";
			$master_profile->delete();
			$master_alamat->delete();
			$murid->delete();
			$profile_bapa->delete();
			$bapa->delete();
			break;
		}

		//create ibu
		$ibu = new Ibubapa();
		foreach ($_REQUEST[ibu] as $k=>$v) {
			$ibu->$k = $v;
			if (empty($v)) $ibu->$k = NULL;
		}
		$ibu->child_profile_id = $master_profile->id;
		$ibu->master_profile_id = $profile_ibu->id;
		$ibu->hubungan_id = '2';
		$ibu->kerja_id = NULL;
		$ibu->pendapatan = NULL;
		if (!$ibu->save()) { 
			$_SESSION['error'] .= "<br>Ibu: ".$ibu->ErrorMsg();
			$act = "new_murid";
			$master_profile->delete();
			$master_alamat->delete();
			$murid->delete();
			$profile_bapa->delete();
			$bapa->delete();
			$profile_ibu->delete();
			break;
		}
		
		//$obj->Load('id='.$_REQUEST['id']);
		$_SESSION['flash'] = 'Murid telah berjaya ditambah.';
		header("Location: ?act=view_murid&nomatrik=$murid->nomatrik");
		break;

	////////////////////////kelas////////////////////////////////
	case "sek_list_kelas" :
		break;
		
	case "list_guru" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		
		$sql = "SELECT p.nama,p.no_kp,g.*,k.nama_kelas FROM $TABLE_GURU g 
		        LEFT JOIN master_kelas k ON k.guru_id=g.staf_id
				INNER JOIN master_profile p ON p.id=g.master_profile_id
				WHERE g.current_institusi_id='$_REQUEST[institusi_id]' 
		        ORDER BY p.nama ";
		$res = pg_query($sql);
		$cnt = pg_num_rows($res);
		//echo $cnt;exit;
		break;

	case "popup_new_guru_to_kelas" :
		$kelas = new Kelas();
		$kelas->Load("id=".$_REQUEST[kelas_id]);
		$sql = "SELECT p.nama,p.no_kp,g.*,k.nama_kelas,k.id as kelas_id FROM $TABLE_GURU g 
		        LEFT JOIN master_kelas k ON k.guru_id=g.staf_id
				INNER JOIN master_profile p ON p.id=g.master_profile_id
				WHERE g.current_institusi_id='$_REQUEST[institusi_id]' 
				  AND g.kod_kategori='01'
		        ORDER BY nama ";
		//$sql = "SELECT * FROM $TABLE_GURU g WHERE g.current_institusi_id='$_REQUEST[institusi_id]' 
		//       ORDER BY g.nama ";
		$res = pg_query($sql);

		include _VIEWPATH_."layout_popup.php";exit;
		break;
		
	case "popup_new_pembantu_to_kelas" :
		$kelas = new Kelas();
		$kelas->Load("id=".$_REQUEST[kelas_id]);
		$sql = "SELECT p.nama,p.no_kp,g.*,k.nama_kelas,k.id as kelas_id FROM $TABLE_GURU g 
		        LEFT JOIN master_kelas k ON k.guru_id=g.staf_id
				INNER JOIN master_profile p ON p.id=g.master_profile_id
				WHERE g.current_institusi_id='$_REQUEST[institusi_id]' 
				   AND g.kod_kategori='02'
		        ORDER BY nama ";
		//$sql = "SELECT * FROM $TABLE_GURU g WHERE g.current_institusi_id='$_REQUEST[institusi_id]' 
		//       ORDER BY g.nama ";
		$res = pg_query($sql);

		include _VIEWPATH_."layout_popup.php";exit;
		break;
		
		
		
	case "search_guru" :

		if (empty($_REQUEST['q'])) {
			$res_count = 0;
		}
		else {
			//set where clause , and pagination url parameter
			$where_arr = array();
			$paginate_arr = array();
			if ($_REQUEST[q] != '') { 
				$where_arr[] = "i.nama ILIKE '%$_REQUEST[q]%'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			if ($_REQUEST[n] != '') { 
				//$where_arr[] = "i.negeri_id = '$_REQUEST[n]'" ;
				$paginate_arr[]	= "n=$_REQUEST[n]";
			}
			if ($_REQUEST[d] != '') { 
				//$where_arr[] = "i.kod_ppd = '$_REQUEST[d]'" ;
				$paginate_arr[]	= "d=$_REQUEST[d]";
			}
			if ($_REQUEST[a] != '') { 
				//$where_arr[] = "i.agensi_pelaksana_id = '$_REQUEST[a]'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			
			//prasekolah is default search for SMPK
			//$where_arr[] = "i.prasekolah = '$_REQUEST[prasekolah]'" ;
			//$where_arr[] = "i.prasekolah = 't'" ;

			$where_clause = "WHERE " . join(" AND ", $where_arr);
			
			
			//echo $where_clause;exit;

			if (empty($_REQUEST['p'])) {
				$page = 1;
				$range = "OFFSET 0 LIMIT 20";
			}
			else {
				$page = $_REQUEST['p'];
				$offset = ($page-1) * 20;
				$range = "OFFSET $offset LIMIT 20";			
			}

			$sql_total_cnt = "SELECT count(staf_id) FROM vw_staf i $where_clause";
			$res = pg_query($sql_total_cnt);
			$dat = pg_fetch_object($res,0);
			$total_count = $dat->count;
			$last_page = floor($total_count/20) + 1;

			$sql = "SELECT i.*
					FROM vw_staf i  $where_clause
					ORDER BY id $range";
					//echo $sql;exit;
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		}
		break;

	case "search_murid" :
		if (empty($_REQUEST['q'])) {
			$res_count = 0;
		}
		else {
			//set where clause , and pagination url parameter
			$where_arr = array();
			$paginate_arr = array();
			if ($_REQUEST[q] != '') { 
				$where_arr[] = "i.nama ILIKE '%$_REQUEST[q]%'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			if ($_REQUEST[n] != '') { 
				//$where_arr[] = "i.negeri_id = '$_REQUEST[n]'" ;
				$paginate_arr[]	= "n=$_REQUEST[n]";
			}
			if ($_REQUEST[d] != '') { 
				//$where_arr[] = "i.kod_ppd = '$_REQUEST[d]'" ;
				$paginate_arr[]	= "d=$_REQUEST[d]";
			}
			if ($_REQUEST[a] != '') { 
				//$where_arr[] = "i.agensi_pelaksana_id = '$_REQUEST[a]'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			
			//prasekolah is default search for SMPK
			//$where_arr[] = "i.prasekolah = '$_REQUEST[prasekolah]'" ;
			//$where_arr[] = "i.prasekolah = 't'" ;

			$where_clause = "WHERE " . join(" AND ", $where_arr);
			
			
			//echo $where_clause;exit;

			if (empty($_REQUEST['p'])) {
				$page = 1;
				$range = "OFFSET 0 LIMIT 20";
			}
			else {
				$page = $_REQUEST['p'];
				$offset = ($page-1) * 20;
				$range = "OFFSET $offset LIMIT 20";			
			}

			$sql_total_cnt = "SELECT count(murid_id) FROM vw_murid i $where_clause";
			$res = pg_query($sql_total_cnt);
			$dat = pg_fetch_object($res,0);
			$total_count = $dat->count;
			$last_page = floor($total_count/20) + 1;

			$sql = "SELECT i.*
					FROM vw_murid i  $where_clause
					ORDER BY id $range";
					//echo $sql;exit;
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		}
		break;

	case "search_application" :
		if (empty($_REQUEST['q'])) {
			$res_count = 0;
		}
		else {
			//set where clause , and pagination url parameter
			$where_arr = array();
			$paginate_arr = array();
			if ($_REQUEST[q] != '') { 
				$where_arr[] = "i.nama ILIKE '%$_REQUEST[q]%'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			if ($_REQUEST[n] != '') { 
				//$where_arr[] = "i.negeri_id = '$_REQUEST[n]'" ;
				$paginate_arr[]	= "n=$_REQUEST[n]";
			}
			if ($_REQUEST[d] != '') { 
				//$where_arr[] = "i.kod_ppd = '$_REQUEST[d]'" ;
				$paginate_arr[]	= "d=$_REQUEST[d]";
			}
			if ($_REQUEST[a] != '') { 
				//$where_arr[] = "i.agensi_pelaksana_id = '$_REQUEST[a]'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			
			//prasekolah is default search for SMPK
			//$where_arr[] = "i.prasekolah = '$_REQUEST[prasekolah]'" ;
			//$where_arr[] = "i.prasekolah = 't'" ;

			$where_clause = "WHERE " . join(" AND ", $where_arr);
			
			
			//echo $where_clause;exit;

			if (empty($_REQUEST['p'])) {
				$page = 1;
				$range = "OFFSET 0 LIMIT 20";
			}
			else {
				$page = $_REQUEST['p'];
				$offset = ($page-1) * 20;
				$range = "OFFSET $offset LIMIT 20";			
			}

			$sql_total_cnt = "SELECT count(id) FROM murid i $where_clause";
			$res = pg_query($sql_total_cnt);
			$dat = pg_fetch_object($res,0);
			$total_count = $dat->count;
			$last_page = floor($total_count/20) + 1;

			$sql = "SELECT i.*
					FROM murid i  $where_clause
					ORDER BY id $range";
					//echo $sql;exit;
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		}
		break;

	case "list_murid_belum_pakai" :
		if (empty($_REQUEST['p'])) {
			$page = 1;
			$range = "OFFSET 0 LIMIT 20";
		}
		else {
			$page = $_REQUEST['p'];
			$offset = ($page-1) * 20;
			$range = "OFFSET $offset LIMIT 20";			
		}

		$sql = "SELECT m.*,n.negeri
		        FROM profiles m 
				LEFT JOIN ref_negeri n ON n.id=m.address_state_id
			    ORDER BY id $range";
		$res = pg_query($sql);
		break;

	case "apply_step1" :
		break;
	case "apply_step2" :
		break;

	case "edit_murid" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$murid = new Murid();
		$murid->Load("nomatrik='$_REQUEST[nomatrik]'");

		$master_profile = new MasterProfile();
		$master_profile->Load('id='.$murid->master_profile_id);

		$master_alamat = new MasterAlamat();
		$master_alamat->Load('master_profile_id='.$murid->master_profile_id . " AND jenis_alamat='1'");
		
		$bapa = new Ibubapa();
		$bapa->Load("child_profile_id=$master_profile->id AND hubungan_id='1'");

		$profile_bapa = new MasterProfile();
		$profile_bapa->Load("id=$bapa->master_profile_id");

		$ibu = new Ibubapa();
		$ibu->Load("child_profile_id=$master_profile->id AND hubungan_id='2'");

		$profile_ibu = new MasterProfile();
		$profile_ibu->Load("id=$ibu->master_profile_id");
		break;

	case "update_murid" :
		//update murid (table: murid)
		$murid = new Murid();
		$murid->Load("nomatrik='$_REQUEST[nomatrik]'");
		foreach ($_REQUEST[murid] as $k => $v) {
			$murid->$k = $v;
			if (empty($v)) $murid->$k = NULL;
		}
		if ($murid->current_kelas_id == '') $murid->current_kelas_id = NULL;

		//$murid->master_profile_id = $master_profile->id;
		//$murid->current_institusi_id = NULL;
		//$murid->current_kelas_id = NULL;
		if (!$murid->save()) { 
			exitError($murid->ErrorMsg());
		}

		//update profile (table: master_profile)
			//echo $murid->master_profile_id;exit;
		$master_profile = new MasterProfile();
		$master_profile->Load('id='.$murid->master_profile_id);
		foreach ($_REQUEST[master_profile] as $k => $v) {
			$master_profile->$k = $v;
		}
		if (empty($master_profile->no_passport)) $master_profile->no_passport = NULL;
		if (empty($master_profile->no_surat_beranak)) $master_profile->no_surat_beranak = NULL;
		$master_profile->tarikh_lahir = ymd_date($master_profile->tarikh_lahir);
			//print_r($master_profile);exit;
		if (!$master_profile->save()) { 
			exitError($master_profile->ErrorMsg());
		}

		//update alamat (table: master_alamat)
		$master_alamat = new MasterAlamat();
		$master_alamat->Load('master_profile_id='.$murid->master_profile_id . " AND jenis_alamat='1'");
		foreach ($_REQUEST[master_alamat] as $k => $v) {
			$master_alamat->$k = $v;
			if (empty($v)) $master_alamat->$k = NULL;
		}
		$master_alamat->jenis_alamat = '1';
		$master_alamat->master_profile_id = $master_profile->id;
		if (!$master_alamat->save()) { 
			exitError($master_alamat->ErrorMsg());
		}
		
		//update bapa (table: parent)
		$bapa = new Ibubapa();
		$bapa->Load("child_profile_id=$master_profile->id AND hubungan_id='1'");
		foreach ($_REQUEST[bapa] as $k=>$v) {
			$bapa->$k = $v;
			if (empty($v)) $bapa->$k = NULL;
		}
		//$bapa->child_profile_id = $master_profile->id;
		//$bapa->master_profile_id = $profile_bapa->id;
		$bapa->hubungan_id = '1';
		//$bapa->kerja_id = NULL;
		$bapa->pendapatan = NULL;
		if (!$bapa->save()) { 
			exitError($bapa->ErrorMsg());
		}

		//update profile bapa (table: master_profile)
		$profile_bapa = new MasterProfile();
		$profile_bapa->Load("id=$bapa->master_profile_id");
			//print_r($profile_bapa);exit;
		foreach ($_REQUEST[profile_bapa] as $k=>$v) {
			$profile_bapa->$k = $v;
			if (empty($v)) $profile_bapa->$k = NULL;
		}
		if (!$profile_bapa->save()) { 
			exitError($profile_bapa->ErrorMsg());
		}
		

		//update ibu (table: parent)
		$ibu = new Ibubapa();
		$ibu->Load("child_profile_id=$master_profile->id AND hubungan_id='2'");
		foreach ($_REQUEST[ibu] as $k=>$v) {
			$ibu->$k = $v;
			if (empty($v)) $ibu->$k = NULL;
		}
		$ibu->child_profile_id = $master_profile->id;
		//$ibu->hubungan_id = '2';
		//$ibu->kerja_id = NULL;
		//$ibu->pendapatan = NULL;
		if (!$ibu->save()) { 
			exitError($bapa->ErrorMsg());
		}

		//update profile ibu (table: master_profile)
		$profile_ibu = new MasterProfile();
		$profile_ibu->Load("id=$ibu->master_profile_id");
		foreach ($_REQUEST[profile_ibu] as $k=>$v) {
			$profile_ibu->$k = $v;
			if (empty($v)) $profile_ibu->$k = NULL;	
		}
				//print_r($profile_ibu);exit;
		if (!$profile_ibu->save()) { 
			exitError($bapa->ErrorMsg());
		}

		
		//echo "ok";exit;

		$_SESSION['flash'] = 'Maklumat murid telah berjaya dikemaskini.';
		header("Location: ?act=view_murid&nomatrik=$murid->nomatrik");
		break;

	case "delete_murid" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$murid = new Murid();
		$murid->Load("nomatrik='$_REQUEST[nomatrik]'");
		
		$murid->current_institusi_id = NULL;
		$murid->current_kelas_id = NULL;
		$murid->save();
		
		header("Location: $_SERVER[HTTP_REFERER]&q=a");
		break;

	case "home" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		break;

	case "laporan_sek" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		break;

	case "laporan_murid" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		break;

	case "list" :
		if (!isset($output) || ($output=='')) $output = 'listing';
		break;

	case "search_sek" :
	case "search_projek" :
		if ($_SESSION['ACCESS_LEVEL'] == '5') {
			$institusi = new Institusi();
			$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");
		}

		if (empty($_REQUEST['q'])) {
			$res_count = 0;
			$sql = "select * from master_institusi limit 50";
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		}
		else {
			//set where clause , and pagination url parameter
			$where_arr = array();
			$paginate_arr = array();
			if ($_REQUEST[q] != '') { 
				$where_arr[] = "i.nama ILIKE '%$_REQUEST[q]%'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			if ($_REQUEST[n] != '') { 
				$where_arr[] = "i.negeri_id = '$_REQUEST[n]'" ;
				$paginate_arr[]	= "n=$_REQUEST[n]";
			}
			if ($_REQUEST[d] != '') { 
				$where_arr[] = "i.kod_ppd = '$_REQUEST[d]'" ;
				$paginate_arr[]	= "d=$_REQUEST[d]";
			}
			if ($_REQUEST[a] != '') { 
				$where_arr[] = "i.agensi_pelaksana_id = '$_REQUEST[a]'" ;
				$paginate_arr[]	= "q=$_REQUEST[q]";
			}
			
			//prasekolah is default search for SMPK
			//$where_arr[] = "i.prasekolah = '$_REQUEST[prasekolah]'" ;
			$where_arr[] = "i.prasekolah = 't'" ;

			$where_clause = "WHERE " . join(" AND ", $where_arr);
			
			
			//echo $where_clause;exit;

			if (empty($_REQUEST['p'])) {
				$page = 1;
				$range = "OFFSET 0 LIMIT 20";
			}
			else {
				$page = $_REQUEST['p'];
				$offset = ($page-1) * 20;
				$range = "OFFSET $offset LIMIT 20";			
			}

			$sql_total_cnt = "SELECT count(institusi_id) FROM master_institusi i $where_clause";
			$res = pg_query($sql_total_cnt);
			$dat = pg_fetch_object($res,0);
			$total_count = $dat->count;
			$last_page = floor($total_count/20) + 1;

			$sql = "SELECT i.institusi_id,i.nama,i.aktif,i.daerah_id,i.alamat,n.negeri,p.ppd,a.agensi_pelaksana FROM master_institusi i 
					 LEFT JOIN ref_negeri n ON n.id=i.negeri_id
					 LEFT JOIN ref_ppd p ON p.kod_ppd=i.kod_ppd
					 LEFT JOIN ref_agensi_pelaksana a ON a.id=i.agensi_pelaksana_id
					 $where_clause 
					 ORDER BY nama $range";
			$sql = "select * from master_institusi limit 50";
			$res = pg_query($sql);
			$res_count = pg_num_rows($res);		
		}
		break;

	case "list_sek" :
		if (empty($_REQUEST['p'])) {
			$page = 1;
			$range = "OFFSET 0 LIMIT 20";
		}
		else {
			$page = $_REQUEST['p'];
			$offset = ($page-1) * 20;
			$range = "OFFSET $offset LIMIT 20";			
		}

		$sql = "SELECT i.institusi_id,i.nama,i.aktif,i.daerah_id,i.alamat,n.negeri,d.daerah,a.kategori_institusi as agensi 
		        FROM master_institusi i 
		         LEFT JOIN ref_negeri n ON n.id=i.negeri_id
		         LEFT JOIN ref_daerah d ON d.kod_daerah=i.kod_daerah
		         LEFT JOIN ref_kategori_institusi a ON a.id=i.kategori_institusi_id
				 WHERE i.negeri_id=n.id ORDER BY id $range";
		//$sql = "select i.id,i.nama,i.aktif,i.daerah_id,i.alamat from master_institusi i"; 
		$res = pg_query($sql);

		break;

	case "view_sek" :
		$institusi = new Institusi();

		if (empty($_REQUEST['institusi_id'])) {
			$kod_institusi = $_REQUEST['kod_institusi'];
			$institusi->Load('kod_institusi='.$kod_institusi);
		}
		else {
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		}
		break;

	case "view_guru" :
		$obj = new MasterStaf();

		$obj->Load('staf_id='.$_REQUEST['id']);
		//print_r($obj);exit;
		break;

	case "view_murid" :
		$institusi = new Institusi();
		$institusi->Load("kod_institusi='$_SESSION[KOD_INSTITUSI]'");

		$murid = new Murid();
		$murid->Load("nomatrik='$_REQUEST[nomatrik]'");

		$master_profile = new MasterProfile();
		$master_profile->Load('id='.$murid->master_profile_id);

		$master_alamat = new MasterAlamat();
		$master_alamat->Load('master_profile_id='.$murid->master_profile_id . " AND jenis_alamat='1'");
		
		$bapa = new Ibubapa();
		$bapa->Load("child_profile_id=$master_profile->id AND hubungan_id='1'");

		$profile_bapa = new MasterProfile();
		$profile_bapa->Load("id=$bapa->master_profile_id");

		$ibu = new Ibubapa();
		$ibu->Load("child_profile_id=$master_profile->id AND hubungan_id='2'");

		$profile_ibu = new MasterProfile();
		$profile_ibu->Load("id=$ibu->master_profile_id");
		break;


	case "list_kelas" :
		$institusi = new Institusi();

		if (empty($_REQUEST['institusi_id'])) {
			$kod = $_REQUEST['kod_institusi'];
			$sql = "select institusi_id from master_institusi where kod_institusi='$kod'";
			$res = pg_query($sql);
			if (pg_num_rows($res)==1) {
				$dat = pg_fetch_object($res,0);
				$institusi->Load('institusi_id='.$dat->id);
			}
		}
		else {
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		}
		
		//get class of institution
		if (isset($_REQUEST['y'])) {
			$the_year = $_REQUEST['y'];
		} else {
			$the_year = Date("Y");
		}

		$sql = "select * from master_kelas where institusi_id='$institusi->institusi_id' AND tahun_mula_operasi<='$the_year' ORDER BY nama_kelas";
		//echo $sql;exit;
		$res_kelas = pg_query($sql);
		break;

	case "edit_kelas" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		
		$kelas = new Kelas();
		$kelas->Load('id='.$_REQUEST['id']);
		break;

	case "view_kelas" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		
		$kelas = new Kelas();
		$kelas->Load('id='.$_REQUEST['id']);
		break;

	case "new_sek_swasta_step1" :
		break;

	case "new_sek_swasta_step2" :
		$institusi = new Institusi();
		$institusi->jenis_institusi_id = $_REQUEST['jenis_institusi_id'];
		break;

	case "new_sek_swasta_step3" :
		break;

	case "new_sek" :
		break;

	case "create_sek_basic" :
		if ($_REQUEST[$OBJECT]['nama']=='') exitError("Sila Masukkan Nama Institusi");
		//check if sub item already exist
		
		//print_r($_REQUEST);
		
		$obj = new Institusi();
		
		foreach ($_REQUEST[$OBJECT] as $k=>$v) {
			$obj->$k = $v;
			if ($obj->$k == '') $obj->$k = NULL;
		}
		if ($_REQUEST['tarikh_ditubuhkan']=='') {
			$obj->tarikh_ditubuhkan = Date("Y-m-d");
		}
		//print_r($obj);exit;
		
		if (!$obj->save()) { 
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Institusi telah diwujudkan.';
		}
		//print_r($obj);exit;
		
		if ($_REQUEST['button_simpan'] == "Simpan & Teruskan") {
			header("Location: ?act=new_sek_maklumat_pra&institusi_id=$institusi->institusi_id");			
		}
		else {
			header("Location: ?act=view_sek&institusi_id=$obj->institusi_id");
		}
		
		//exit;
		/*
		$x = $obj->jenis_institusi_id; 
		if (($x == '18') or ($x == '30') or ($x == '31')) {
			header("Location: ?act=new_sek_swasta_step3&kod=$obj->kod");	
		}
		else {
			header("Location: ?act=view_sek&kod=$obj->kod");
		}
		*/
		break;
		
	case "create_sek_maklumat_pra" :
		print_r($_REQUEST);
		
		$obj = new Institusi();
		
		foreach ($_REQUEST[$OBJECT] as $k=>$v) {
			$obj->$k = $v;
			if ($obj->$k == '') $obj->$k = NULL;
		}
		if ($_REQUEST['tarikh_ditubuhkan']=='') {
			$obj->tarikh_ditubuhkan = Date("Y-m-d");
		}

		$prasekolah = new MaklumatPrasekolah();

		foreach ($_REQUEST['master_institusi_prasekolah'] as $k=>$v) {
			$prasekolah->$k = $v;
			if ($prasekolah->$k == '') $prasekolah->$k = NULL; 
		}
		if ($_REQUEST['tarikh_ditubuhkan']=='') {
			$prasekolah->tarikh_ditubuhkan = ymd_date($_REQUEST['tarikh_ditubuhkan']);
		}
		//print_r($institusi);exit;
		
		if (!$prasekolah->save()) { 
			exitError($prasekolah->ErrorMsg());
			$_SESSION['flash'] = $prasekolah->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Maklumat Prasekolah Institusi telah dikemaskini.';
			if ($_REQUEST['button_simpan'] == "Simpan & Teruskan") {
				header("Location: ?act=edit_sek_maklumat_syarikat&institusi_id=$institusi->institusi_id");			
			}
			else {
				header("Location: ?act=view_sek_maklumat_pra&institusi_id=$institusi->institusi_id");
			}
		}
		
		break;
		
	case "edit_sek" :
		$institusi = new Institusi();

		if (empty($_REQUEST['institusi_id'])) {
			exitError("Institusi tidak wujud");
		}
		else {
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
		}
		break;
		
	case "view_sek_maklumat_pra" :
		if (empty($_REQUEST['institusi_id'])) {
			exitError("Institusi tidak wujud");
		}
		else {
			$institusi = new Institusi();
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
	
			$prasekolah = new MaklumatPrasekolah();
			$prasekolah->Load('master_institusi_id='.$institusi->institusi_id);		
		}
		break;
		
	case "view_sek_maklumat_syarikat" :
		if (empty($_REQUEST['institusi_id'])) {
			exitError("Institusi tidak wujud");
		}
		else {
			$institusi = new Institusi();
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
	
			$organisasi = new Organisasi();
			$organisasi->Load('organisasi_id='.$institusi->organisasi_id);		
		}
		break;
		
	case "edit_sek_maklumat_pra" :
		if (empty($_REQUEST['institusi_id'])) {
			exitError("Institusi tidak wujud");
		}
		else {
			$institusi = new Institusi();
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
	
			$prasekolah = new MaklumatPrasekolah();
			$prasekolah->Load('master_institusi_id='.$institusi->institusi_id);		
		}
		break;
		
	case "edit_sek_maklumat_syarikat" :
		if (empty($_REQUEST['institusi_id'])) {
			exitError("Institusi tidak wujud");
		}
		else {
			$institusi = new Institusi();
			$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);
	
			$organisasi = new Organisasi();
			$organisasi->Load('organisasi_id='.$institusi->organisasi_id);
		}
		break;
		
	case "update_sek" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);		

		foreach ($_REQUEST['master_institusi'] as $k=>$v) {
			$institusi->$k = $v;
			if ($institusi->$k == '') $institusi->$k = NULL; 
		}
		if ($_REQUEST['tarikh_ditubuhkan']=='') {
			$institusi->tarikh_ditubuhkan = ymd_date($_REQUEST['tarikh_ditubuhkan']);
		}
		//print_r($institusi);exit;
		
		if (!$institusi->save()) { 
			exitError($institusi->ErrorMsg());
			exit;
			$_SESSION['flash'] = $institusi->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Institusi telah dikemaskini.';
			if ($_REQUEST['button_simpan'] == "Simpan & Teruskan") {
				header("Location: ?act=edit_sek_maklumat_pra&institusi_id=$institusi->institusi_id");			
			}
			else {
				header("Location: ?act=view_sek&institusi_id=$institusi->institusi_id");
			}
		}
		break;

	case "update_sek_maklumat_pra" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);

		$prasekolah = new MaklumatPrasekolah();
		$prasekolah->Load('master_institusi_id='.$_REQUEST['institusi_id']);
		
		foreach ($_REQUEST['master_institusi_prasekolah'] as $k=>$v) {
			$prasekolah->$k = $v;
			if ($prasekolah->$k == '') $prasekolah->$k = NULL; 
		}
		$prasekolah->masa_operasi_mula = "$_REQUEST[masa_operasi_mula_hour]:$_REQUEST[masa_operasi_mula_minute]";
		$prasekolah->masa_operasi_tamat = "$_REQUEST[masa_operasi_tamat_hour]:$_REQUEST[masa_operasi_tamat_minute]";
		
		//print_r($prasekolah);exit;
		
		if (!$prasekolah->save()) { 
			exitError($prasekolah->ErrorMsg());
			$_SESSION['flash'] = $prasekolah->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Maklumat Prasekolah Institusi telah dikemaskini.';
			if ($_REQUEST['button_simpan'] == "Simpan & Teruskan") {
				header("Location: ?act=edit_sek_maklumat_syarikat&institusi_id=$institusi->institusi_id");			
			}
			else {
				header("Location: ?act=view_sek_maklumat_pra&institusi_id=$institusi->institusi_id");
			}
		}
		break;

	case "update_sek_maklumat_syarikat" :
		//print_r($_REQUEST);exit;
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['institusi_id']);

		foreach ($_REQUEST['master_institusi'] as $k=>$v) {
			$institusi->$k = $v;
		}
		$institusi->save();
		

		if ($institusi->organisasi_id != '') {
			$organisasi = new Organisasi();
			$organisasi->Load('organisasi_id='.$institusi->organisasi_id);
		}
		else {
			$organisasi = new Organisasi();
		}
		
		foreach ($_REQUEST['organisasi'] as $k=>$v) {
			$organisasi->$k = $v;
		}
		if ($organisasi->tarikh_daftar != '') $organisasi->tarikh_daftar = ymd_date($organisasi->tarikh_daftar);
		if ($organisasi->tarikh_tamat_daftar != '') $organisasi->tarikh_tamat_daftar = ymd_date($organisasi->tarikh_tamat_daftar);

		if (!$organisasi->save()) { 
			exitError($organisasi->ErrorMsg());
			$_SESSION['flash'] = $organisasi->ErrorMsg();
		}else {
				//print_r($organisasi);exit;
			$institusi->organisasi_id = $organisasi->organisasi_id;
			//print_r($institusi);exit;
			$institusi->save();
				
			$_SESSION['flash'] = 'Maklumat Syarikat telah dikemaskini.';
			header("Location: ?act=view_sek_maklumat_syarikat&institusi_id=$institusi->institusi_id");
		}
		break;

	case "confirm_sek" :
		break;

	case "delete_sek" :
		//if school has any link to other object, cannot delete
		$sql = "SELECT * from master_kelas WHERE institusi_id='$_REQUEST[id]'";
		$res = pg_query($sql);
		if (pg_num_rows($res) > 0) {
			exitError("Rekod Institusi tidak boleh dihapuskan kerana masih mempunyai kelas. Sila hapuskan rekod kelas dahulu.");
		}
		
		
		$sql = "DELETE from master_institusi where id='$_REQUEST[id]'";
		if ($res = pg_query($sql)) {
			$_SESSION['flash'] = 'Institusi telah dipadam.';
			header("Location: ?act=search_sek");		
		}
		else {
			exitError(pg_last_error());
		}

		break;

	case "new_kelas" :
		$institusi = new Institusi();
		$institusi->Load('institusi_id='.$_REQUEST['sek_id']);
		
		$kelas = new Kelas();

		break;

	case "edit_kelas" :
		$kelas = new Kelas();

		$kelas->Load('id='.$_REQUEST['id']);
		break;

	case "update_kelas" :
		$kelas = new Kelas();
		$kelas->Load('id='.$_REQUEST['id']);		

		foreach ($_REQUEST['master_kelas'] as $k=>$v) {
			$kelas->$k = $v;
		}
		$kelas->institusi_id = $_REQUEST[institusi_id];
		$kelas->is_preschool = '1';
		
		if ($kelas->guru_id == '') {
			$kelas->guru_id = null;
		}
		if ($kelas->pembantu_id == '') {
			$kelas->pembantu_id = null;
		}
//		print_r($kelas);exit;
		
		if (!$kelas->save()) { 
			exitError($kelas->ErrorMsg());
			exit;
			$_SESSION['flash'] = $kelas->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Kelas telah dikemaskini.';
			header("Location: ?act=list_kelas&id=$kelas->institusi_id");
		}
		break;

	case "create_kelas_for_institusi" :
		print_r($_REQUEST);
		if ($_REQUEST['master_kelas']['nama_kelas']=='') exitError("Sila Masukkan Nama Kelas");
		//check if sub item already exist
				
		$obj = new Kelas();
		
		foreach ($_REQUEST['master_kelas'] as $k=>$v) {
			$obj->$k = $v;
		}
		$obj->institusi_id = $_REQUEST[institusi_id];
		$obj->is_preschool = '1';
		
		if ($obj->guru_id == '') {
			$obj->guru_id = null;
		}
		if ($obj->pembantu_id == '') {
			$obj->pembantu_id = null;
		}
		//print_r($obj);exit;
		
		if (!$obj->save()) { 
			echo $obj->ErrorMsg();exit;
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Kelas telah diwujudkan.';
		}
		//exit;
		header("Location: ?act=list_kelas&id=$_REQUEST[institusi_id]");
		break;
		



	default :
		//include _VIEWPATH_."home.php";
		$act = "home";

}
include _VIEWPATH_."$layout.php";

?>
